//
//  GWAccountTool.m
//  新浪微博
//
//  Created by qianfeng on 16/1/13.
//  Copyright (c) 2016年 高炜. All rights reserved.
//
#define PATH [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"account.data"]

#import "GWAccountTool.h"
#import "GWAccount.h"
#import "GWHttpTool.h"
#import "MJExtension.h"
@implementation GWAccountTool
+(void)saveAccount:(GWAccount *)account
{
    
    [NSKeyedArchiver archiveRootObject:account toFile:PATH];
}
+(GWAccount *)Account
{
    GWAccount *account =[NSKeyedUnarchiver unarchiveObjectWithFile:PATH];
    NSDate *nowDate =[NSDate date];
    if ([nowDate compare:account.expires_time]==NSOrderedDescending) {
        account=nil;
    }
   return account;
}

+(void)accountWithParame:(GWAccountParam *)pareme success:(void (^)(GWAccount *))success failure:(void (^)(NSError *))failure
{
    
    [GWHttpTool post:@"https://api.weibo.com/oauth2/access_token" parameters:pareme.keyValues success:^(id respondObj) {
        
        if (success) {
            GWAccount *result =[GWAccount objectWithKeyValues:respondObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (error) {
            failure(error);
        }
    }];
}
@end
